/*
    Dominate 3D - A cube-based game of life

    Copyright (C) 2007, Craig H. Miller (kidmosey@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "player.h"
#include "option.h"

#include <list>

#include <GL/gl.h>

extern std::list<Color> g_AvailableColors;

Player Player::nullPlayer(Color(0.2f, 0.2f, 0.2f));

Player::Player()
        : m_taken(0), m_total(0), m_timerScore(0), m_id(0)
{
    m_numTurns  = 0;
    m_totalTime = 0.0f;
}

Player::Player(const Color &color)
        : c(color), m_taken(0), m_total(0), m_timerScore(0), m_id(0)
{
    m_numTurns  = 0;
    m_totalTime = 0.0f;
}

Player::Player(const std::string &name, const Color &color)
        : c(color), m_taken(0), m_total(0), m_timerScore(0), m_id(0), m_name(name)
{
    m_numTurns  = 0;
    m_totalTime = 0.0f;
}

Player::Player(const std::string &name)
        : m_taken(0), m_total(0), m_timerScore(0), m_id(0), m_name(name)
{
    c = g_AvailableColors.back();
    g_AvailableColors.pop_back();

    m_numTurns  = 0;
    m_totalTime = 0.0f;
}

Player::~Player()
{
    if (m_id)
        g_AvailableColors.push_back(c);
}

void Player::ApplyInverse() const
{
    glColor4f(1.0-c.r, 1.0-c.g, 1.0-c.b, c.a);
}

void Player::Apply() const
{
    glColor4f(c.r, c.g, c.b, c.a);
}

void Player::SetName(const std::string &name)
{
    m_name = name;
}

const std::string &Player::GetName() const
{
    return m_name;
}

int Player::GetScore() const
{
    int score = 0;

    if (OptionIsEnabled("Score_TotalCells"))
        score += m_total;

    if (OptionIsEnabled("Score_TakenCells"))
        score += m_taken;

    if (OptionIsEnabled("Score_UseTimer") &&
        OptionIsEnabled("Score_TimerBased"))
    {
        if (m_numTurns)
        {
            float limit = (float )m_numTurns * (float )atoi(GetOption("Score_TimeLimit").c_str());
            score += (int)(limit / m_totalTime * 2.0f);
        }
    }

    return score;
}

const Color COLORS[] =
{
/* Red  	 */  Color((long )0xFF0000FF),
//* AliceBlue  	 */  Color((long )0xF0F8FFFF),
//* Aqua  	 */  Color((long )0x00FFFFFF),
//* Aquamarine  	 */  Color((long )0x7FFFD4FF),
/* Blue  	 */  Color((long )0x0000FFFF),
//* BlueViolet  	 */  Color((long )0x8A2BE2FF),
//* Brown  	 */  Color((long )0xA52A2AFF),
//* CadetBlue  	 */  Color((long )0x5F9EA0FF),
//* Chartreuse  	 */  Color((long )0x7FFF00FF),
//* Crimson  	 */  Color((long )0xDC143CFF),
//* Cyan  	 */  Color((long )0x00FFFFFF),
/* DarkCyan  	 */  Color((long )0x008B8BFF),
//* DarkGoldenRod  	 */  Color((long )0xB8860BFF),
//* DarkMagenta  	 */  Color((long )0x8B008BFF),
/* Darkorange  	 */  Color((long )0xFF8C00FF),
//* DarkOrchid  	 */  Color((long )0x9932CCFF),
/* DarkRed  	 */  Color((long )0x8B0000FF),
//* DarkSalmon  	 */  Color((long )0xE9967AFF),
//* DarkTurquoise  	 */  Color((long )0x00CED1FF),
/* DarkViolet  	 */  Color((long )0x9400D3FF),
//* DeepPink  	 */  Color((long )0xFF1493FF),
//* DeepSkyBlue  	 */  Color((long )0x00BFFFFF),
//* Fuchsia  	 */  Color((long )0xFF00FFFF),
/* Gold  	 */  Color((long )0xFFD700FF),
//* GoldenRod  	 */  Color((long )0xDAA520FF),
//* HoneyDew  	 */  Color((long )0xF0FFF0FF),
//* HotPink  	 */  Color((long )0xFF69B4FF),
//* IndianRed   	 */  Color((long )0xCD5C5CFF),
//* Indigo   	 */  Color((long )0x4B0082FF),
//* Lavender  	 */  Color((long )0xE6E6FAFF),
//* LavenderBlush  	 */  Color((long )0xFFF0F5FF),
/* LightBlue  	 */  Color((long )0xADD8E6FF),
//* LightCyan  	 */  Color((long )0xE0FFFFFF),
/* LightGoldenRodYellow  	 */  Color((long )0xFAFAD2FF),
/* LightPink  	 */  Color((long )0xFFB6C1FF),
//* LightSalmon  	 */  Color((long )0xFFA07AFF),
//* LightSkyBlue  	 */  Color((long )0x87CEFAFF),
//* LightSlateGrey  	 */  Color((long )0x778899FF),
//* LightSteelBlue  	 */  Color((long )0xB0C4DEFF),
/* LightYellow  	 */  Color((long )0xFFFFE0FF),
/* Magenta  	 */  Color((long )0xFF00FFFF),
/* Maroon  	 */  Color((long )0x800000FF),
/* MediumAquaMarine  	 */  Color((long )0x66CDAAFF),
/* MediumBlue  	 */  Color((long )0x0000CDFF),
/* MediumOrchid  	 */  Color((long )0xBA55D3FF),
/* MediumPurple  	 */  Color((long )0x9370D8FF),
/* MediumSlateBlue  	 */  Color((long )0x7B68EEFF),
/* MediumTurquoise  	 */  Color((long )0x48D1CCFF),
/* MediumVioletRed  	 */  Color((long )0xC71585FF),
/* MistyRose  	 */  Color((long )0xFFE4E1FF),
/* Navy  	 */  Color((long )0x000080FF),
/* Olive  	 */  Color((long )0x808000FF),
/* OliveDrab  	 */  Color((long )0x6B8E23FF),
/* Orange  	 */  Color((long )0xFFA500FF),
/* Orchid  	 */  Color((long )0xDA70D6FF),
/* PaleGoldenRod  	 */  Color((long )0xEEE8AAFF),
/* PaleTurquoise  	 */  Color((long )0xAFEEEEFF),
/* PaleVioletRed  	 */  Color((long )0xD87093FF),
/* PeachPuff  	 */  Color((long )0xFFDAB9FF),
/* Pink  	 */  Color((long )0xFFC0CBFF),
/* Plum  	 */  Color((long )0xDDA0DDFF),
/* PowderBlue  	 */  Color((long )0xB0E0E6FF),
/* Purple  	 */  Color((long )0x800080FF),
/* Red  	 */  Color((long )0xFF0000FF),
/* Salmon  	 */  Color((long )0xFA8072FF),
/* Sienna  	 */  Color((long )0xA0522DFF),
/* SkyBlue  	 */  Color((long )0x87CEEBFF),
/* SlateBlue  	 */  Color((long )0x6A5ACDFF),
/* SlateGrey  	 */  Color((long )0x708090FF),
/* SteelBlue  	 */  Color((long )0x4682B4FF),
/* Teal  	 */  Color((long )0x008080FF),
/* Thistle  	 */  Color((long )0xD8BFD8FF),
/* Tomato  	 */  Color((long )0xFF6347FF),
/* Turquoise  	 */  Color((long )0x40E0D0FF),
/* Violet  	 */  Color((long )0xEE82EEFF),
/* Wheat  	 */  Color((long )0xF5DEB3FF),
/* Yellow  	 */  Color((long )0xFFFF00FF)
};

std::list<Color> g_AvailableColors(COLORS, COLORS+(sizeof(COLORS)/sizeof(*COLORS)));
